"""
产品页面类
"""
from framwork.pages.base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException


class ProductPage(BasePage):
    url = '/crm/index.php?m=product'
    btn_add_product_locator = (
        By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(2) > div.pull-right > a')
    tips_success_locator = (By.CSS_SELECTOR, 'body > div.container > div.alert.alert-success')
    first_product_name_locator = (
    By.CSS_SELECTOR, '#form1 > table > tbody > tr:nth-child(1) > td:nth-child(3) > a > span')

    choose_gx_locator = (By.NAME, 'product_id[]')
    delete_btn_locator = (By.ID, 'delete')
    delete_success_locator = (By.CSS_SELECTOR, 'body > div.container > div.alert.alert-success')

    def click_gxk(self):
        '''点击勾选'''
        self.find_element(self.choose_gx_locator).click()

    def click_delete(self):
        '''点击删除'''
        self.find_element(self.delete_btn_locator).click()

    def click_btn_add_product(self):
        '''点击添加产品按钮'''
        self.find_element(self.btn_add_product_locator).click()

    def delete_success_text(self):
        '''删除成功，提示文本，用于断言'''
        try:
            text = self.find_element(self.delete_success_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def tips_success_text(self):
        try:
            text = self.find_element(self.tips_success_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def first_product_name(self):
        return self.find_element(self.first_product_name_locator).text
